package com.bjpowernode.threadpool.shutdown;

import com.bjpowernode.util.ThreadUtils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test03 {

    public static void main(String[] args) {
        doSomeThing();
    }

    public static void doSomeThing() {
        //基于Executor框架实现线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                8,
                16,
                15,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(1024),
                Executors.defaultThreadFactory(),
                //Executors.privilegedThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        for (int i = 0; i < 10; i++) {
            threadPoolExecutor.execute(() -> {
                ThreadUtils.sleepMilliSeconds(200);
                System.out.println("线程执行任务......");
            });
        }

        //关闭线程池
        threadPoolExecutor.shutdown();

        try {
            while (!threadPoolExecutor.awaitTermination(100, TimeUnit.MILLISECONDS)) {
                System.out.println("线程池还在运行......");
            }
        } catch (java.lang.InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程池已经完全关闭......");
    }
}
